//
//  PerformanceFilterVM.swift
//  kxf
//
//  Created by Mac on 2024/3/24.
//

import Foundation
import UIKit

class PerformanceFilterVM: UIView {
    
    let selfHeight = kFitWidth(110)
    var controller = WHBaseViewVC()
    
    var type = "day"
    var timeType = "start"
    
    var startTime = ""
    var endTime = ""
    
    var queryBlock:((String,String)->())?
    
    override init(frame:CGRect){
        super.init(frame: CGRect.init(x: frame.origin.x, y: frame.origin.y, width: SCREEN_WIDHT, height: selfHeight))
        self.backgroundColor = .clear
        self.isUserInteractionEnabled = true
        
        initUI()
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    lazy var dataTitleVm : PerformanceNoticeVM = {
        let vm = PerformanceNoticeVM.init(frame: CGRect.init(x: 0, y: kFitWidth(10), width: 0, height: 0))
        vm.titleVm.titleLabel.text = "商户数据"
        vm.contentLabel.text = "\(Date().getLast30Days())  -  \(Date().todayDate)"
        vm.arrowImgView.setImgLocal(imgName: "arrow_img_down")
        vm.contentLabel.textColor = .COLOR_TEXT_BLACK999
        vm.contentLabel.font = .systemFont(ofSize: 16, weight: .regular)
        vm.contentLabel.isUserInteractionEnabled = true
        self.startTime = Date().getLast30Days()
        self.endTime = Date().todayDate
//        let tap = UITapGestureRecognizer.init(target: self, action: #selector(dateTapActinon))
//        vm.addGestureRecognizer(tap)
        
        return vm
    }()
    
    lazy var tapView : UIView = {
        let vi = UIView.init(frame: CGRect.init(x: 0, y: kFitWidth(10), width: SCREEN_WIDHT, height: RegisterViewHeadTitleVM().selfHeight))
        vi.backgroundColor = .clear
        vi.isUserInteractionEnabled = true
        
        let tap = UITapGestureRecognizer.init(target: self, action: #selector(dateTapActinon))
        vi.addGestureRecognizer(tap)
        
        return vi
    }()
    lazy var typeBottomView : UIView = {
        let vi = UIView()
        vi.backgroundColor = WHColor_16(colorStr: "E4E4E4")
        vi.layer.cornerRadius = kFitWidth(14)
        vi.clipsToBounds = true
        vi.isUserInteractionEnabled = true
        
        let tap = UITapGestureRecognizer.init(target: self, action: #selector(typeChangeAction))
        vi.addGestureRecognizer(tap)
        
        return vi
    }()
    lazy var whiteView : UIView = {
        let vi = UIView()
        vi.backgroundColor = .white
        vi.layer.cornerRadius = kFitWidth(11)
        vi.clipsToBounds = true
        
        return vi
    }()
    lazy var dayLabel : UILabel = {
        let lab = UILabel()
        lab.text = "按日"
        lab.textColor = .black
        lab.font = .systemFont(ofSize: 12, weight: .regular)
        lab.textAlignment = .center
        
        return lab
    }()
    lazy var monthLabel : UILabel = {
        let lab = UILabel()
        lab.text = "按月"
        lab.textColor = .black
        lab.font = .systemFont(ofSize: 12, weight: .regular)
        lab.textAlignment = .center
        
        return lab
    }()
    lazy var timeFilterAlertVm : PerformanceTimeFilterAlertVM = {
        let vm = PerformanceTimeFilterAlertVM.init(frame: .zero)
        vm.isHidden = true
        vm.startTimeBtn.tapBlock = {()in
            if self.type == "month"{
                self.datePickerMonthVm.isHidden = false
            }else{
                self.datePickerVm.isHidden = false
                self.datePickerVm.datePicker.minimumDate = nil
                self.datePickerVm.datePicker.maximumDate = Date().changeDateStringToDate(dateStr: self.endTime)
                self.datePickerVm.datePicker.setDate(Date().changeDateStringToDate(dateStr: self.startTime), animated: true)
            }
        }
        vm.endTimeBtn.tapBlock = {()in
            if self.type == "month"{
                self.datePickerMonthVm.isHidden = false
            }else{
                self.datePickerVm.isHidden = false
                self.datePickerVm.datePicker.minimumDate = Date().changeDateStringToDate(dateStr: self.startTime)
                self.datePickerVm.datePicker.maximumDate = Date()
                self.datePickerVm.datePicker.setDate(Date().changeDateStringToDate(dateStr: self.endTime), animated: true)
            }
        }
        vm.resetBlock = {()in
            if self.type == "month"{
                self.startTime = "\(self.timeFilterAlertVm.startTimeBtn.timeLabel.text ?? "")-01"
                self.endTime = "\(self.timeFilterAlertVm.endTimeBtn.timeLabel.text ?? "")-01"
            }else{
                self.startTime = "\(self.timeFilterAlertVm.startTimeBtn.timeLabel.text ?? "")"
                self.endTime = "\(self.timeFilterAlertVm.endTimeBtn.timeLabel.text ?? "")"
            }
        }
        
        return vm
    }()
    
    lazy var datePickerVm : DatePickerVM = {
        let vm = DatePickerVM.init(frame: .zero)
        vm.isHidden = true
        vm.datePicker.maximumDate = Date()
        vm.timeConfirmBlock = {(time)in
            if self.timeType == "start"{
                self.startTime = time
                self.timeFilterAlertVm.startTimeBtn.timeLabel.text = "\(time)"
                self.timeFilterAlertVm.startTimeBtn.timeString = "\(time)"
                self.timeType = "end"
                self.datePickerVm.isHidden = false
                self.datePickerVm.datePicker.minimumDate = Date().changeDateStringToDate(dateStr: time)
                self.datePickerVm.datePicker.maximumDate = Date()
                self.datePickerVm.titleLabel.text = "选择查询结束日期"
            }else{
                self.endTime = time
                self.timeFilterAlertVm.endTimeBtn.timeLabel.text = "\(time)"
                self.timeFilterAlertVm.endTimeBtn.timeString = "\(time)"
            }
        }
        return vm
    }()
    lazy var datePickerMonthVm: DatePickerMonthVM = {
        let vm = DatePickerMonthVM.init(frame: .zero)
        vm.isHidden = true
        vm.timeConfirmBlock = {(time)in
            if self.timeType == "start"{
                self.startTime = "\(time)-01"
                self.timeFilterAlertVm.startTimeBtn.timeLabel.text = "\(time)"
                self.timeFilterAlertVm.startTimeBtn.timeString = "\(time)"
                self.timeType = "end"
                self.datePickerMonthVm.isHidden = false
                self.datePickerVm.titleLabel.text = "选择查询结束日期"
            }else{
                self.endTime = "\(time)-01"
                self.timeFilterAlertVm.endTimeBtn.timeLabel.text = "\(time)"
                self.timeFilterAlertVm.endTimeBtn.timeString = "\(time)"
            }
        }
        return vm
    }()
}

extension PerformanceFilterVM{
    @objc func typeChangeAction() {
        if type == "day"{
            type = "month"
            self.timeFilterAlertVm.type = type
            self.timeFilterAlertVm.startTimeBtn.timeLabel.text = "\(self.startTime.mc_clipFromPrefix(to: 7))"
            self.timeFilterAlertVm.endTimeBtn.timeLabel.text = "\(self.endTime.mc_clipFromPrefix(to: 7))"
            self.dataTitleVm.contentLabel.text = "\(self.startTime.mc_clipFromPrefix(to: 7))  -  \(self.endTime.mc_clipFromPrefix(to: 7))"
            whiteView.snp.remakeConstraints { make in
                make.right.equalTo(kFitWidth(-3))
                make.centerY.lessThanOrEqualToSuperview()
                make.width.equalTo(kFitWidth(40))
                make.height.equalTo(kFitWidth(22))
            }
        }else{
            type = "day"
            self.timeFilterAlertVm.type = type
            self.dataTitleVm.contentLabel.text = "\(self.startTime)  -  \(self.endTime)"
            self.timeFilterAlertVm.startTimeBtn.timeLabel.text = "\(self.startTime)"
            self.timeFilterAlertVm.endTimeBtn.timeLabel.text = "\(self.endTime)"
            whiteView.snp.remakeConstraints { make in
                make.left.equalTo(kFitWidth(3))
                make.centerY.lessThanOrEqualToSuperview()
                make.width.equalTo(kFitWidth(40))
                make.height.equalTo(kFitWidth(22))
            }
        }
        if self.queryBlock != nil{
            self.queryBlock!(self.timeFilterAlertVm.startTimeBtn.timeLabel.text ?? "",self.timeFilterAlertVm.endTimeBtn.timeLabel.text ?? "")
        }
    }
    @objc func dateTapActinon(){
        self.timeFilterAlertVm.isHidden = false
    }
}

extension PerformanceFilterVM{
    func initUI() {
        addSubview(dataTitleVm)
        addSubview(tapView)
        
        addSubview(typeBottomView)
        typeBottomView.addSubview(whiteView)
        typeBottomView.addSubview(dayLabel)
        typeBottomView.addSubview(monthLabel)
        
        UIApplication.shared.keyWindow?.addSubview(timeFilterAlertVm)
        UIApplication.shared.keyWindow?.addSubview(datePickerVm)
        UIApplication.shared.keyWindow?.addSubview(datePickerMonthVm)
        
        setConstrait()
    }
    func setConstrait() {
        typeBottomView.snp.makeConstraints { make in
            make.left.equalTo(kFitWidth(16))
            make.top.equalTo(kFitWidth(72))
            make.width.equalTo(kFitWidth(85))
            make.height.equalTo(kFitWidth(28))
        }
        whiteView.snp.makeConstraints { make in
            make.left.equalTo(kFitWidth(3))
            make.centerY.lessThanOrEqualToSuperview()
            make.width.equalTo(kFitWidth(40))
            make.height.equalTo(kFitWidth(22))
        }
        dayLabel.snp.makeConstraints { make in
            make.left.equalTo(kFitWidth(3))
            make.top.width.height.equalTo(whiteView)
        }
        monthLabel.snp.makeConstraints { make in
            make.right.equalTo(kFitWidth(-3))
            make.top.width.height.equalTo(dayLabel)
        }
    }
}
